import threading, Queue, random, thread,time
from math import *
from human_control import *
from agent_controller import *
from graphics_engine import *
from world_model import *
from agent import *
from sensor_range_finder import *
from sensor_pie_slices import *
from sensor_radar import *
# Initialize world, game, IO, control_queue,etc
control_queue=Queue.Queue()
main_graphics_engine=graphics_engine()
main_graphics_engine.start_event_loop()
main_world_model=world_model(main_graphics_engine,[(160,60,160,180),(80,120,240,120),(160,360,480,360),(480,60,480,360)])

# Create Agents, put them in a dict indexed by number, pass them to agent_controller, initialize them with world_model
temp_agents={}
temp_actions={'move_rel_world':main_world_model.agent_move_rel_world,'move_rel_agent':main_world_model.agent_move_rel_agent,'turn_rel':main_world_model.agent_turn_rel}
for i in xrange(20):
	range_finder=sensor_range_finder(i, main_world_model, [-15*pi/180, 0, 15*pi/180], 200)
	pie_slices=sensor_pie_slices(i,main_world_model,[-3*pi/4,-pi/4,pi/4,3*pi/4], 100)
	radar=sensor_radar(i, main_world_model, 100)
	temp_agents[i]=agent(actions=temp_actions,senses={'range_finder':range_finder.sense, 'radar':radar.sense, 'pie_slices':pie_slices.sense},agent_num=i)
	main_world_model.new_agent(i)

main_agent_controller=agent_controller(temp_agents)
main_human_control=human_control(tk_root=main_graphics_engine.tk_root,control_queue=control_queue,agent_controller=main_agent_controller,world_model=main_world_model)
# This is the main thread's processing loop
while main_graphics_engine.mainloop_thread.isAlive():
	# Process human input events
	try:
		temp_fun=control_queue.get(block=False)
		temp_fun()
	except Queue.Empty:
		pass
main_graphics_engine.mainloop_thread.join()
